package com.bysj.lms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bysj.lms.pojo.entity.Permission;

import com.bysj.lms.pojo.entity.Role;
import com.bysj.lms.mapper.PermissionMapper;
import com.bysj.lms.service.IPermissionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bysj.lms.service.IRolePermissionService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xiaojiakai
 * @since 2022-02-04
 */
@Service
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, Permission> implements IPermissionService {

    @Autowired
    PermissionMapper permissionMapper;
    @Autowired
    IRolePermissionService rolePermissionService;

    @Override
    public PageInfo findByPage(String title, int page, int count) {
        PageHelper.startPage(page,count);
        QueryWrapper<Permission> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(title) && StringUtils.isNotEmpty(title),"title",title);
        List<Permission> permissions = permissionMapper.selectList(queryWrapper);
        return new PageInfo(permissions);
    }

    @Override
    public List<Permission> getByRoleId(List<Role> roles) {
        Set<Integer> pidByRid = rolePermissionService.getPidByRid(roles);
        List<Permission> permissions = listByIds(pidByRid);
        return permissions;
    }
}
